﻿using System;
using System.Collections.Generic;
using System.Text;
using MYLSOFT.JsonSimple;
using System.Xml;

namespace com.byt.gestor.service
{
    class VuceToGestor
    {

        private static Dictionary<string, string> names;

        public static JSONObject ProcesarDGS005(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            if(nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));


            nl = n.SelectNodes("LaboratoryLocation");
            if (nl.Count > 0)
                jo.Add("LaboratoryLocation", LaboratoryLocation_l(nl));


            nl = n.SelectNodes("FactoryLocation");
            if (nl.Count > 0)
                jo.Add("FactoryLocation", FactoryLocation(nl[0]));

            nl = n.SelectNodes("StorageLocation");
            if (nl.Count > 0)
                jo.Add("StorageLocation", StorageLocation(nl[0]));

            nl = n.SelectNodes("FoodIndustrialProduct");
            if (nl.Count > 0)
                jo.Add("FoodIndustrialProduct", FoodIndustrialProduct_s(nl));

            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocument", GetObject("ExchangeDocument", nl[0]));


            return jo;
        }

        public static JSONObject ProcesarDGS006(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));

            nl = n.SelectNodes("FactoryLocation");
            jo.Add("FactoryLocation", FactoryLocation(nl[0]));

            nl = n.SelectNodes("FoodIndustrialProduct");
            jo.Add("FoodIndustrialProduct", FoodIndustrialProduct_s(nl));

            nl = n.SelectNodes("AttachedDocument");
            jo.Add("AttachedDocument", AttachedDocument(nl));

            return jo;
        }

        public static JSONObject ProcesarDGS007(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            nl = n.SelectNodes("SanitaryRegisterHolder");
            if (nl.Count > 0)
            {
                jo.Add("SanitaryRegisterHolder", SanitaryRegisterHolder_dgs007(nl[0]));
            }
               
            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocuments", GetObject("ExchangeDocument", nl[0]));


            return jo;
        }

        public static JSONObject ProcesarDGS008(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));


            nl = n.SelectNodes("LaboratoryLocation");
            if (nl.Count > 0)
                jo.Add("LaboratoryLocation", LaboratoryLocation_l(nl));


            nl = n.SelectNodes("FactoryLocation");
            if (nl.Count > 0)
                jo.Add("FactoryLocation", FactoryLocation(nl[0]));

            nl = n.SelectNodes("StorageLocation");
            if (nl.Count > 0)
                jo.Add("StorageLocation", StorageLocation(nl[0]));

            nl = n.SelectNodes("FoodIndustrialProduct");
            if (nl.Count > 0)
                jo.Add("FoodIndustrialProduct", FoodIndustrialProduct_s(nl));

            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocuments", GetObject("ExchangeDocument", nl[0]));


            return jo;
        }

        /* AGREGADO POR CESAR BOCANEGRA */

        public static JSONObject ProcesarDGS009(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            /*  AGREGADO POR MODIFICACION  */

            nl = n.SelectNodes("ChangedPetitioner");
            if (nl.Count > 0)
                jo.Add("ChangedPetitioner", ChangedPetitioner(nl[0]));


            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));


            nl = n.SelectNodes("LaboratoryLocation");
            if (nl.Count > 0)
                jo.Add("LaboratoryLocation", LaboratoryLocation_l(nl));


            nl = n.SelectNodes("FactoryLocation");
            if (nl.Count > 0)
                jo.Add("FactoryLocation", FactoryLocation(nl[0]));

            nl = n.SelectNodes("StorageLocation");
            if (nl.Count > 0)
                jo.Add("StorageLocation", StorageLocation(nl[0]));

            nl = n.SelectNodes("FoodIndustrialProduct");
            if (nl.Count > 0)
                jo.Add("FoodIndustrialProduct", FoodIndustrialProduct_s(nl));

            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            /*modificado por CESAR BOCANEGRA */
            nl = n.SelectNodes("AttachedDocuments");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

           /* nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocuments", GetObject("ExchangeDocument", nl[0]));
            */

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocument", GetObject("ExchangeDocument", nl[0]));

            return jo;
        }

        /* AGREGADO POR CESAR BOCANEGRA */

        public static JSONObject ProcesarDGS002(XmlDocument xml)
        {
            LoadNames();
            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;


            //tag documentHeader
            XmlNodeList nl = n.SelectNodes("DocumentHeader");

            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            //tag EstablishmentLocation
            nl = n.SelectNodes("EstablishmentLocation");
            if (nl.Count > 0)
                jo.Add("EstablishmentLocation", EstablishmentLocation(nl[0]));

            
            /*
            //tag SpecificInformation
            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));
            */

            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", SpecificInformation(nl[0]));


            //tag FoodDrinkProduct
            nl = n.SelectNodes("FoodDrinkProduct");
            if (nl.Count > 0)
                jo.Add("FoodDrinkProduct", FoodDrinkProduct_s(nl));

            //tag ProductDestination
            /*
            nl = n.SelectNodes("ProductDestination");
            if (nl.Count > 0)
                jo.Add("ProductDestination", ProductDestination_s(nl));
             */
            nl = n.SelectNodes("ProductDestination");
            if (nl.Count > 0)
                jo.Add("ProductDestination", ProductDestination(nl[0]));

            //tag de Adjuntos
            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("AttachedDocuments");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocument", GetObject("ExchangeDocument", nl[0]));

            
            return jo;
        }

        /* AGREGADO POR CESAR BOCANEGRA PROCESO DE DGS004 */

        public static JSONObject ProcesarDGS004(XmlDocument xml)
        {
            LoadNames();
            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            //DocumentHeader
            XmlNodeList nl = n.SelectNodes("DocumentHeader");

            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            //SanitaryRegisterHolder

            nl = n.SelectNodes("SanitaryRegisterHolder");
            if (nl.Count > 0)
                jo.Add("SanitaryRegisterHolderXX", SanitaryRegisterHolder1(nl[0]));


            //ProductDestination
            nl = n.SelectNodes("ProductDestination");
            if (nl.Count > 0)
                jo.Add("ProductDestination", ProductDestination(nl[0]));

            //SpecificInformation
            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", SpecificInformation(nl[0]));

            //SanitaryRegister
            nl = n.SelectNodes("SanitaryRegister");
            if (nl.Count > 0)
                jo.Add("SanitaryRegister", SanitaryRegister(nl));

            //AttachedDocument(s)
            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("AttachedDocuments");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocument", GetObject("ExchangeDocument", nl[0]));


            return jo;
        }

        /* FIN DE MODIFICACION */

        /* AGREGADO POR CESAR BOCANEGRA PROCESO DE DGS003 */

        public static JSONObject ProcesarDGS003(XmlDocument xml)
        {
            LoadNames();
            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            //DocumentHeader
            XmlNodeList nl = n.SelectNodes("DocumentHeader");

            if (nl.Count > 0)
                jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            //StorageLocation

            nl = n.SelectNodes("StorageLocation");
            if (nl.Count > 0)
                jo.Add("StorageLocation", StorageLocation(nl[0]));

            //SanitaryRegisterHolder

            nl = n.SelectNodes("SanitaryRegisterHolder");
            if (nl.Count > 0)
                jo.Add("SanitaryRegisterHolder", SanitaryRegisterHolder(nl[0]));

            //AttachedDocument(s)
            nl = n.SelectNodes("AttachedDocument");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("AttachedDocuments");
            if (nl.Count > 0)
                jo.Add("AttachedDocument", AttachedDocument(nl));

            nl = n.SelectNodes("ExchangeDocument");
            if (nl.Count > 0)
                jo.Add("ExchangeDocument", GetObject("ExchangeDocument", nl[0]));

            return jo;
        }

        /* FIN */ 

        public static JSONObject ProcesarN34(XmlDocument xml)
        {
            LoadNames();

            JSONObject jo = new JSONObject();
            XmlNode n = xml.DocumentElement;

            XmlNodeList nl = n.SelectNodes("DocumentHeader");
            jo.Add("DocumentHeader", DocumentHeader(nl[0]));

            nl = n.SelectNodes("SpecificInformation");
            if (nl.Count > 0)
                jo.Add("SpecificInformation", GetObject("SpecificInformation", nl[0]));

            nl = n.SelectNodes("FactoryLocation");
            jo.Add("FactoryLocation", FactoryLocation(nl[0]));

            nl = n.SelectNodes("FoodIndustrialProduct");
            jo.Add("FoodIndustrialProduct", FoodIndustrialProduct_s(nl));

            nl = n.SelectNodes("AttachedDocument");
            jo.Add("AttachedDocument", AttachedDocument(nl));

            return jo;
        }

        private static void LoadNames()
        {
            // Cargar diccionario de nombres
            names = new Dictionary<string, string>();

            //Tipo de tramite
            names.Add("SpecificInformation.EnrollmentFlag", "inscripcion");
            names.Add("SpecificInformation.ReEnrollmentFlag", "reinscripcion");
            //**//
            //Documento Inicial
            names.Add("SpecificInformation.OriginalDRYear", "drano");
            names.Add("SpecificInformation.OriginalDRNumber", "drnro");
            names.Add("SpecificInformation.OriginalSUCEYear", "suceano");
            names.Add("SpecificInformation.OriginalSUCENumber", "sucenro");
            names.Add("SpecificInformation.CertifiedNumber", "certificado");
            names.Add("SpecificInformation.FileNumber", "expediente");
            names.Add("SpecificInformation.ChangePetitioner", "tcambempresa");//8
            names.Add("SpecificInformation.ChangeLegalRepresentative", "tcambrepresentante");//8
            names.Add("SpecificInformation.ChangeStorageLocation", "tcambalmacenamiento");//8
            names.Add("SpecificInformation.ChangeProductDescription", "tcambdenominacion");//8
            names.Add("SpecificInformation.ChangeLogisticsPackage", "tcambenvase");//8
            names.Add("SpecificInformation.ChangeTaste", "tcambformulacion");//8
            names.Add("SpecificInformation.CancellationFlag", "tcambcancelacion");//8
            names.Add("SpecificInformation.ChangeFactoryLocation", "tcamblugarfabrica");//9 modificado por CESAR BOCANEGRA
            names.Add("SpecificInformation.DocumentLanguage.LanguageCode", "itemidioma");
            names.Add("SpecificInformation.DocumentLanguage.ItemDocumentLanguage", "itemidioma");
            names.Add("SpecificInformation.DocumentLanguage.Language", "nomidioma");

            //laboratorio de acreditacion
            names.Add("LaboratoryLocation.Item", "laboratorioacreditadoitem");
            names.Add("LaboratoryLocation.TradeParty.Type", "idlaboratorioacreditadotipo");
            names.Add("LaboratoryLocation.TradeParty.Identification", "ididlaboratorioacreditado");
            names.Add("LaboratoryLocation.TradeParty.Name", "nombre");

            //Solicitante ó Titular del Registro
            names.Add("DocumentHeader.PetitionerTradeParty.TelephoneUniversalCommunication", "soltelffijo");//8
            names.Add("DocumentHeader.PetitionerTradeParty.FaxUniversalCommunication", "soltelffax");//8
            names.Add("DocumentHeader.PetitionerTradeParty.MobileTelephoneUniversalCommunication", "soltelfcelular");//8
            names.Add("DocumentHeader.PetitionerTradeParty.Identification", "solnumedocumento");//8
            names.Add("DocumentHeader.PetitionerTradeParty.Name", "solnombpersona");//8
            names.Add("DocumentHeader.PetitionerTradeParty.Ubigeo", "solidubigeo");//8
            names.Add("DocumentHeader.PetitionerTradeParty.Type", "solidtipopersona");//8
            names.Add("DocumentHeader.PetitionerTradeParty.IdentificationType", "solidtipodocu");//8
            names.Add("DocumentHeader.PetitionerTradeParty.TradeAddress", "soldirepersona"); //8 pero falta el ultimo "StreetName."
            names.Add("DocumentHeader.PetitionerTradeParty.Reference", "soldirereferencia");
            names.Add("DocumentHeader.PetitionerTradeParty.EmailUniversalCommunication", "soldireemail");//8
            names.Add("DocumentHeader.PetitionerTradeParty.LegalRepresentativeIdentification", "repnumedocumento");//8
            names.Add("DocumentHeader.PetitionerTradeParty.LegalRepresentativeName", "repnombpersona");//8
            names.Add("DocumentHeader.PetitionerTradeParty.LegalRepresentativeIdentificationType", "repidtipodocu");//8
            names.Add("DocumentHeader.PetitionerTradeParty.LegalRepresentativeTradeAddress", "repdirepersona");//8

            names.Add("DocumentHeader.PersonTradeParty.TelephoneUniversalCommunication", "perlabtelffijo");
            names.Add("DocumentHeader.PersonTradeParty.FaxUniversalCommunication", "perlabtelffax");
            names.Add("DocumentHeader.PersonTradeParty.MobileTelephoneUniversalCommunication", "perlabtelfcelular");
            names.Add("DocumentHeader.PersonTradeParty.Identification", "perlabnumedocumento");
            names.Add("DocumentHeader.PersonTradeParty.Name", "perlabnombpersona");
            names.Add("DocumentHeader.PersonTradeParty.Ubigeo", "perlabidubigeo");
            names.Add("DocumentHeader.PersonTradeParty.Type", "perlabidtipopersona");
            names.Add("DocumentHeader.PersonTradeParty.IdentificationType", "perlabidtipodocu");
            names.Add("DocumentHeader.PersonTradeParty.TradeAddress", "perlabdirepersona");
            names.Add("DocumentHeader.PersonTradeParty.EmailUniversalCommunication", "perlabdireemail");

            names.Add("DocumentHeader.CustomsAgentTradeParty.Identification", "labnumedocumento");
            names.Add("DocumentHeader.CustomsAgentTradeParty.Name", "labnombpersona");
            names.Add("DocumentHeader.CustomsAgentTradeParty.Ubigeo", "labidubigeo");
            names.Add("DocumentHeader.CustomsAgentTradeParty.IdentificationType", "labidtipodocu");
            names.Add("DocumentHeader.CustomsAgentTradeParty.TradeAddress", "labdirepersona");
            //dgs008

           /* names.Add("DocumentHeader.ChangedPetitionerTradeParty.Name", "cambempresa");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.TradeAddress", "cambdireccion");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.TelephoneUniversalCommunication", "cambtelefono");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.FaxUniversalCommunication", "cambfax");
            */
            names.Add("ChangedPetitioner.PetitionerTradeParty.Name", "cambempresa");
            names.Add("ChangedPetitioner.PetitionerTradeParty.TradeAddress", "cambdireccion");
            names.Add("ChangedPetitioner.PetitionerTradeParty.TelephoneUniversalCommunication", "cambtelefono");
            names.Add("ChangedPetitioner.PetitionerTradeParty.FaxUniversalCommunication", "cambfax");

            names.Add("ChangedPetitioner.PetitionerTradeParty.LegalRepresentativeIdentificationType", "cambtdocumentorepre");
            names.Add("ChangedPetitioner.PetitionerTradeParty.LegalRepresentativeIdentification", "cambndocumentorepre");
            names.Add("ChangedPetitioner.PetitionerTradeParty.LegalRepresentativeName", "cambnombrerepre");
            names.Add("ChangedPetitioner.PetitionerTradeParty.LegalRepresentativeTradeAddress", "cambdireccionrepre");

            //dgs008

            /*names.Add("DocumentHeader.ChangedPetitionerTradeParty.LegalRepresentativeIdentificationType", "cambtdocumentorepre");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.LegalRepresentativeIdentification", "cambndocumentorepre");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.LegalRepresentativeName", "cambnombrerepre");
            names.Add("DocumentHeader.ChangedPetitionerTradeParty.LegalRepresentativeTradeAddress", "cambdireccionrepre");
            */

            names.Add("FactoryLocation.TradeCountry.Code", "idpais");//8
            names.Add("FactoryLocation.TradeCountry.Name", "nombpais");//8
            names.Add("FactoryLocation.TradeParty.Name", "nombpersona");//8
            names.Add("FactoryLocation.TradeParty.IdentificationType", "idtipodocu");
            names.Add("FactoryLocation.TradeParty.Identification", "numedocumento");
            names.Add("FactoryLocation.TradeParty.TradeAddress", "direpersona");//8
            names.Add("FactoryLocation.TradeParty.Ubigeo", "ubigeo");
            names.Add("FactoryLocation.TradeParty.TelephoneUniversalCommunication", "telffijo");
            names.Add("FactoryLocation.TradeParty.FaxUniversalCommunication", "telffax");
            names.Add("FactoryLocation.TradeParty.EmailUniversalCommunication", "direemail");
            names.Add("FactoryLocation.TradeParty.Reference", "direreferencia");
            names.Add("FactoryLocation.TradeParty.LegalRepresentativeName", "repnombpersona");
            names.Add("FactoryLocation.TradeParty.LegalRepresentativeIdentificationType", "repidtipodocu");
            names.Add("FactoryLocation.TradeParty.LegalRepresentativeIdentification", "repnumedocumento");
            names.Add("FactoryLocation.TradeParty.Type", "idtipopersona");
            names.Add("FactoryLocation.TradeParty.FileNumber", "expedienteplanta");
            names.Add("FactoryLocation.TradeParty.Resolution", "resolucionplanta");
            names.Add("FactoryLocation.CertificationAuthority", "nombautoridad");
            names.Add("FactoryLocation.QualityControlResponsible", "qaresp");//QualityControlResponsible DGS006
            names.Add("FactoryLocation.FileNumber", "texpplanta");//2 9 modificado por CESAR BOCANEGRA
            names.Add("FactoryLocation.Resolution", "tnroresolucion"); //2 9 modificado por CESAR BOCANEGRA

            //names.Add("StorageLocation.TradeParty.Type", "prodfracc");//DGS009 CB
            names.Add("StorageLocation.TradeParty.EmailUniversalCommunication", "almdireemail");//8
            names.Add("StorageLocation.TradeParty.FaxUniversalCommunication", "almtelffax");//8
            names.Add("StorageLocation.TradeParty.Identification", "almnumedocumento");//8
            names.Add("StorageLocation.TradeParty.LegalRepresentativeIdentification", "repnumedocumento");//8
            names.Add("StorageLocation.TradeParty.LegalRepresentativeIdentificationType", "repidtipodocu");//8
            names.Add("StorageLocation.TradeParty.LegalRepresentativeName", "repnombpersona");//8
            names.Add("StorageLocation.TradeParty.Name", "almnombpersona");//8
            names.Add("StorageLocation.TradeParty.Reference", "almdirereferencia");
            names.Add("StorageLocation.TradeParty.TelephoneUniversalCommunication", "almtelffijo");//8
            names.Add("StorageLocation.TradeParty.TradeAddress", "almdirepersona");//8
            names.Add("StorageLocation.TradeParty.Ubigeo", "almidubigeo");//8
            names.Add("StorageLocation.TradeParty.IdentificationType", "tipodocfabric");//9 modificado por CESAR BOCANEGRA
            names.Add("StorageLocation.TradeParty.Type", "idtipopersona");
            names.Add("StorageLocation.TradeParty.FileNumber", "expedienteplanta");
            names.Add("StorageLocation.TradeParty.Resolution", "resolucionplanta");
            names.Add("StorageLocation.UbigeoDetail.Department", "estbldepartamento");//DGS003 CB
            names.Add("StorageLocation.UbigeoDetail.Province", "estblprovincia");//DGS003 CB
            names.Add("StorageLocation.UbigeoDetail.District", "estbldistrito");//DGS003 CB

            names.Add("FoodIndustrialProduct.ItemProduct", "numeitem");//8
            names.Add("FoodIndustrialProduct.Name", "nombproducto");//8
            names.Add("FoodIndustrialProduct.RegisterCode", "registrosanitario");//8

            names.Add("FoodIndustrialProduct.TradeName", "denominacioncomercial");//6

            names.Add("FoodIndustrialProduct.BrandName", "marcproducto");
            names.Add("FoodIndustrialProduct.StorageConditions", "condconsalma");
            names.Add("FoodIndustrialProduct.LifePeriod", "perivida");
            names.Add("FoodIndustrialProduct.ProductIdentificationSystem", "sistlote");
            names.Add("FoodIndustrialProduct.SanitaryRegistryOriginProductRegisterData.RegisterIdentifier", "autosanitaria");
            names.Add("FoodIndustrialProduct.TradeProduct.TariffCode", "idsubpartida");//8
            names.Add("FoodIndustrialProduct.TradeProduct.TariffCodeSubGroup", "codipartida");//8
            names.Add("FoodIndustrialProduct.StateFlag", "estadoregistro");//8
            names.Add("FoodIndustrialProduct.CanceledFlag", "cancelaregistro");//8
            names.Add("FoodIndustrialProduct.ProductClassification.Code", "codiclase");//8
            names.Add("FoodIndustrialProduct.ProductClassification.Group", "codigrupo");//8
            names.Add("FoodIndustrialProduct.ProductClassification.Name", "nombclase");//8
            names.Add("FoodIndustrialProduct.Composition.Description", "desccomposicion");
            names.Add("FoodIndustrialProduct.Composition.ItemComposition", "itemcomposicion");//8            
            names.Add("FoodIndustrialProduct.Composition.OriginalFormulation", "descorigcomposicion");//8
            names.Add("FoodIndustrialProduct.Composition.CurrentFormulation", "descnuevcomposicion");//8            
            names.Add("FoodIndustrialProduct.AdditiveProduct.AlimentaryCode", "sincodex");
            names.Add("FoodIndustrialProduct.AdditiveProduct.GrasSustance", "sustgrasa");
            names.Add("FoodIndustrialProduct.AdditiveProduct.ItemAdditive", "itemaditivo");
            names.Add("FoodIndustrialProduct.AdditiveProduct.TradeName", "descaditivo");
            names.Add("FoodIndustrialProduct.AdditiveProduct.Weight", "pesoaditivo");
            names.Add("FoodIndustrialProduct.LogisticsPackage.Capacity", "capaenvase");//8
            names.Add("FoodIndustrialProduct.LogisticsPackage.ItemPackage", "itemenvase");//8
            names.Add("FoodIndustrialProduct.LogisticsPackage.Material", "mateenvase");//8
            names.Add("FoodIndustrialProduct.LogisticsPackage.Type", "tipoenvase");//8

            names.Add("FoodDrinkProduct.Description", "descrip_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.ScientificName", "nom_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.Type", "embalaje_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.LogisticsPackage.Type", "cant_embalada");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.ProductFormat", "pres_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.NetWeight", "pneto_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.GrossWeight", "pbruto_producto");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.StorageTemperature.Value", "temp_alma_producto");//DGS002 CESAR BOCANEGRA
            //names.Add("FoodDrinkProduct.ExpiryDate", "fech_vence_producto");//DGS002 CESAR BOCANEGRA original 1
            names.Add("FoodDrinkProduct.Container", "nro_contenedor");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.InvoiceInformation.InvoiceNumber", "factura_comercial");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.VesselName", "nom_embarque");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.DetailProduct.Batch", "nrolote");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.DetailProduct.ProductionDate", "fech_produccion");//DGS002 CESAR BOCANEGRA
            names.Add("FoodDrinkProduct.DetailProduct.ExpiryDate", "fech_vence_producto");//DGS002 CESAR BOCANEGRA modificado

            names.Add("AttachedDocument.AttachedDate", "fecarchivo");
            names.Add("AttachedDocument.AttachedName", "nombarchivo");
            names.Add("AttachedDocument.DocumentName", "itemarchivo");//DGS008
            
            names.Add("ExchangeDocument.Description", "respuesta");
            
            //names.Add("EstablishmentLocation.TradeParty.TradeAddress.StreetName", "direstablecimiento");
            names.Add("EstablishmentLocation.TradeParty.Name", "nomestablecimiento");
            names.Add("EstablishmentLocation.TradeParty.TradeAddress", "direstablecimiento");
            names.Add("EstablishmentLocation.TradeParty.Identification", "codvuceestable");

            names.Add("ProductDestination.OriginPort", "penvio");
            names.Add("ProductDestination.DestinationPort", "pdestino");
            names.Add("ProductDestination.TradeTransportMode.Type", "mediotransporte");
            names.Add("ProductDestination.LoadingDate", "fchembarque");
            names.Add("ProductDestination.SenderTradeParty.Name", "nom_exped");
            names.Add("ProductDestination.SenderTradeParty.TradeAddress", "expdireccion");
            names.Add("ProductDestination.ConsigneeTradeParty.Name", "nomdestinatario");
            names.Add("ProductDestination.ConsigneeTradeParty.TradeAddress", "direc_destino");
            names.Add("ProductDestination.AdditionalNotes", "notif_adicional");
            names.Add("ProductDestination.DestinationTradeCountry.Code", "codpaisdest");
            names.Add("ProductDestination.DestinationTradeCountry.Name", "nompaisdest");
            
            names.Add("SanitaryRegisterHolder.TradeParty.Identification", "numedocumento");//value igual al de factory location       
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.FactoryLocation.TradeParty.Name", "nombpersona");//value igual al de factory location 
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.FactoryLocation.TradeParty.TradeAddress", "direpersona");//value igual al de factory location 
            names.Add("SanitaryRegisterHolder.TradeParty.Name", "nomtitucertificado");//DGS003 CESAR BOCANEGRA // dgs007
            names.Add("SanitaryRegisterHolder.TradeParty.TradeAddress", "direpersona");//DGS003 CESAR BOCANEGRA // dgs007
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.Number", "numecertif");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.ItemSanitaryRegister", "itemvuce");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.Code", "codregsanitario");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.IssueDate", "emisioncertif");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.ExpirationDate", "fincertif");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.Name", "nomprod");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.Manufacturer", "nomfabricante");//DGS007
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.UsefulLife", "vidautil");//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.TradeProduct.TariffCode", "parsunat"); //partida arancelaria SUNAT//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.TradeProduct.PhysicalMeasureUnit", "measureunit"); //segun excel no procesar//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.TradeProduct.PhysicalQuantity", "pquantity"); //segun excel no procesar//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.TradeProduct.TariffCodeSubGroup", "coddetpartida"); //Codigo Detalle de Partida//DGS003 CESAR BOCANEGRA
            names.Add("SanitaryRegisterHolder.OriginTradeCountry.Code", "procpaisorigen"); //Procedencia (Pais de Origen)
            names.Add("SanitaryRegisterHolder.OriginTradeCountry.Name", "paisorigen"); //Codigo Detalle de Partida

            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.ProductClassification.Code", "codiclase");//8
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.ProductClassification.Group", "codigrupo");//8
            names.Add("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.ProductClassification.Name", "nombclase");//8



            names.Add("SanitaryRegister.ItemSanitaryRegister", "numeitem");//
            names.Add("SanitaryRegister.Code", "registrosanitario");//
            names.Add("SanitaryRegister.SanitaryCertificateNumber", "numecertif");//
            names.Add("SanitaryRegister.IssueDate", "emisioncertif");//
            names.Add("SanitaryRegister.ExpirationDate", "fincertif");//
            names.Add("SanitaryRegister.FoodProduct.Name", "nombproducto");//
            names.Add("SanitaryRegister.FoodProduct.Manufacturer", "nomfabricante");//
            names.Add("SanitaryRegister.FoodProduct.UsefulLife", "perivida");//
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.TariffCode", "codipartida");//
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.PhysicalMeasureUnit", "measureunit");//
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.PhysicalQuantity", "pquantity");//
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.TariffCodeSubGroup", "idsubpartida");//

            //names.Add("ProductDestination.SenderTradeParty.TradeAddress.StreetName", "expdireccion");
            //names.Add("ProductDestination.ConsigneeTradeParty.TradeAddress.StreetName", "direc_destino");

            //names.Add("FoodDrinkProduct.DetailsProduct.Batch", "nrolote");

            //names.Add("FoodDrinkProduct.DetailsProduct.ProductionDate", "fech_produccion");

            // names.Add("FoodDrinkProduct.ExpiryDate", "fech_vencimiento"); //se repite el tag

            //names.Add("SpecificInformation.DocumentLanguage", "idioma");
            //names.Add("SanitaryRegisterHolder.SanitaryCertificate.FactoryLocation.TradeParty.TradeAddress.StreetName","direcfabrica");
            /*
            names.Add("SanitaryRegister.ItemSanitaryRegister", "itemvuce");
            names.Add("SanitaryRegister.Code", "codregsanitario");
            names.Add("SanitaryRegister.SanitaryCertificateNumber", "numecertif");
            names.Add("SanitaryRegister.IssueDate", "emisioncertif");
            names.Add("SanitaryRegister.ExpirationDate", "fincertif");
            names.Add("SanitaryRegister.FoodProduct.Name", "nomprod");
            names.Add("SanitaryRegister.FoodProduct.Manufacturer", "nomfabricante");
            names.Add("SanitaryRegister.FoodProduct.UsefulLife", "vidautil");
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.TariffCode", "parsunat");
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.PhysicalMeasureUnit", "measureunit");
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.PhysicalQuantity", "pquantity");
            names.Add("SanitaryRegister.FoodProduct.TradeProduct.TariffCodeSubGroup", "coddetpartida");
             */
   
        }



        private static object SanitaryRegisterHolder_dgs007(XmlNode n)
        {


            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;
            JSONArray ja = new JSONArray();

            nl = n.SelectNodes("TradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("SanitaryRegisterHolder.TradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            nl = n.SelectNodes("OriginTradeCountry");
            if (nl.Count > 0)
            {
                ch = GetObject("SanitaryRegisterHolder.OriginTradeCountry", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            nl = n.SelectNodes("SanitaryCertificate");
            if (nl.Count > 0)
            {
                XmlNode n3 = nl[0].ChildNodes[0];
                jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate" + "." + n3.Name), n3.InnerText);

                XmlNodeList n2 = nl[0].SelectNodes("SanitaryRegister");
                XmlNodeList n4;

                foreach (XmlNode it in n2)
                {

                    jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.ItemSanitaryRegister"), it.ChildNodes[0].InnerText);
                    jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.Code"), it.ChildNodes[1].InnerText);
                    jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.IssueDate"), it.ChildNodes[2].InnerText);
                    jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.ExpirationDate"), it.ChildNodes[3].InnerText);

                    JSONObject jox = new JSONObject();

                    n4 = it.SelectNodes("FoodProduct");

                    ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct", n4[0]);
                    foreach (string key in ch.Keys)
                        jox.Add(key, ch[key]);
                    //RemoveNode(n4[0], n4);

                    ja.Add(jox);

                }
                jo.Add("productos", ja);


            }



            return jo;

        }



        
        private static object LaboratoryLocation_l(XmlNodeList nl)
        {
            JSONObject jo = new JSONObject();
            JSONArray ja = new JSONArray();
            JSONObject ch;
            foreach (XmlNode n in nl)
            {

                JSONObject jox = new JSONObject();
                XmlNode n3 = n.ChildNodes[0];
                jox.Add(GetName("LaboratoryLocation" + "." + n3.Name), n3.InnerText);

                XmlNodeList n4 = n.SelectNodes("TradeParty");

                ch = GetObject("LaboratoryLocation.TradeParty", n4[0]);
                foreach (string key in ch.Keys)
                    jox.Add(key, ch[key]);

                
                ja.Add(jox);


            }
            //jo.Add("items", ja);
            return ja;

        }
        
        private static JSONObject ChangedPetitioner(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;
            nl = n.SelectNodes("PetitionerTradeParty");
            ch=GetObject("ChangedPetitioner.PetitionerTradeParty",nl[0]);
            foreach (string key in ch.Keys)
                jo.Add(key, ch[key]);

            return jo;
        }

        private static object LaboratoryLocation(XmlNode n)
        {

            JSONObject jo = new JSONObject();
            XmlNodeList nl;

            XmlNode n3 = n.ChildNodes[0];
            jo.Add(GetName("LaboratoryLocation" + "." + n3.Name), n3.InnerText);


            nl = n.SelectNodes("TradeParty");


            jo.Add("TradeParty", GetArray("LaboratoryLocation", nl));
            RemoveNode(n, nl);
            return jo;
        }

        private static JSONObject DocumentHeader(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject child = null;
            foreach (XmlNode it in n.ChildNodes)
            {
                if (it.ChildNodes.Count == 1 && it.ChildNodes[0] is XmlCDataSection)
                {
                    jo.Add(GetName("DocumentHeader." + it.Name), it.InnerText);
                }
                else
                {
                    child = GetObject("DocumentHeader." + it.Name, it);
                    foreach (string key in child.Keys)
                        jo.Add(key, child[key]);
                }

            }


            return jo;
        }

        private static JSONObject FactoryLocation(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject child = null;

            foreach (XmlNode it in n.ChildNodes)
            {
                if (it.ChildNodes.Count == 1 && it.ChildNodes[0] is XmlCDataSection)
                {
                    jo.Add(GetName("FactoryLocation." + it.Name), it.InnerText);
                }
                else
                {
                    child = GetObject("FactoryLocation." + it.Name, it);
                    foreach (string key in child.Keys)
                        jo.Add(key, child[key]);
                }

            }

            return jo;
        }

        private static object StorageLocation(XmlNode n)
        {

            /* MODIFICADO POR CESAR BOCANEGRA */
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;

            // TradeParty
            nl = n.SelectNodes("TradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("StorageLocation.TradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            // UbigeoDetail
            nl = n.SelectNodes("UbigeoDetail");
            if (nl.Count > 0)
            {
                ch = GetObject("StorageLocation.UbigeoDetail", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            return jo;
            

           /* if (n.ChildNodes.Count > 0)
            {
                n = n.ChildNodes[0];    // TradeParty
                return GetObject("StorageLocation.TradeParty", n);
            }
            return new JSONObject();*/

            /* FIN DE LA MODIFICACION */ 

        }


        private static object SanitaryRegisterHolder1(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            XmlNodeList nl;
            JSONObject ch;
            

            nl = n.SelectNodes("TradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("SanitaryRegisterHolder.TradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            nl = n.SelectNodes("SanitaryCertificate");
            if (nl.Count > 0)
            {
                XmlNodeList nl2 = nl[0].SelectNodes("FactoryLocation");
                XmlNodeList nl3 = nl2[0].SelectNodes("TradeParty");
                ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate.FactoryLocation.TradeParty",nl3[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            return jo;
        }

/* MODIFICADO POR CESAR BOCANEGRA */
        private static object SanitaryRegisterHolder(XmlNode n)
        {
            /* MODIFICADO POR CESAR BOCANEGRA */

            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;
            JSONArray ja = new JSONArray();

            // TradeParty
            nl = n.SelectNodes("TradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("SanitaryRegisterHolder.TradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            // OriginTradeCountry
            nl = n.SelectNodes("OriginTradeCountry");
            if (nl.Count > 0)
            {
                ch = GetObject("SanitaryRegisterHolder.OriginTradeCountry", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //SanitaryCertificate
            nl = n.SelectNodes("SanitaryCertificate");
            if (nl.Count > 0)
            {
                XmlNode n3 = nl[0].ChildNodes[0];                
                jo.Add(GetName("SanitaryRegisterHolder.SanitaryCertificate" + "." + n3.Name), n3.InnerText);
                //ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate", nl[0]);

                XmlNodeList n2 = nl[0].SelectNodes("SanitaryRegister");
                XmlNodeList n4,n5;

                //ja = GetArray("SanitaryRegisterHolder.SanitaryCertificate", n2);
                //RemoveNode(nl[0], n2);
                //jo.Add("productos", ja); //con estos funciona salvo el problema del foodproduct q es una iteraccion mas
                 
                foreach (XmlNode it in n2)
                {
                    JSONObject jox = new JSONObject();
                    n4 = it.SelectNodes("FoodProduct");
                    n5 = n4[0].SelectNodes("TradeProduct");

                    ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct.TradeProduct", n5[0]);
                    foreach (string key in ch.Keys)
                        jox.Add(key, ch[key]);
                    RemoveNode(n4[0], n5);


                    ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister.FoodProduct", n4[0]);
                    foreach (string key in ch.Keys)
                        jox.Add(key, ch[key]);
                        RemoveNode(it, n4);

                    ch = GetObject("SanitaryRegisterHolder.SanitaryCertificate.SanitaryRegister", it);
                    foreach (string key in ch.Keys)
                        jox.Add(key, ch[key]);

                    ja.Add(jox);

                }
                jo.Add("productos", ja);
                

            }

            

            return jo;

            /*
            if (n.ChildNodes.Count > 0)
            {
                n = n.ChildNodes[0]; //TradePArty
                return GetObject("SanitaryRegisterHolder.TradeParty", n);
            }
            return new JSONObject();*/

            /* FIN DE LA MODIFICACION */ 
        }

        private static object EstablishmentLocation(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject child = null;
            foreach (XmlNode it in n.ChildNodes)
            {
                if (it.ChildNodes.Count == 1 && it.ChildNodes[0] is XmlCDataSection)
                {
                    jo.Add(GetName("EstablishmentLocation." + it.Name), it.InnerText);
                }
                else
                {
                    child = GetObject("EstablishmentLocation." + it.Name, it);
                    foreach (string key in child.Keys)
                        jo.Add(key, child[key]);
                }

            }


            return jo;
        }

        private static JSONObject ProductDestination(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;           

            // TradeTransportMode
            nl = n.SelectNodes("TradeTransportMode");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.TradeTransportMode", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            // SenderTradeParty
            nl = n.SelectNodes("SenderTradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.SenderTradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }
                       
            // ConsigneeTradeParty
            nl = n.SelectNodes("ConsigneeTradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.ConsigneeTradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //DestinationTradeCountry
            nl = n.SelectNodes("DestinationTradeCountry");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.DestinationTradeCountry", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }


            // Los items restantes
            ch = GetObject("ProductDestination", n);
            foreach (string key in ch.Keys)
                jo.Add(key, ch[key]);
  
            return jo;
        }

        private static JSONArray FoodDrinkProduct_s(XmlNodeList nl)
        {
            JSONArray ja = new JSONArray();
            foreach (XmlNode n in nl)
            {
                ja.Add(FoodDrinkProduct(n));
            }

            return ja;
        }

        //MODIFICADO POR CESAR BOCANEGRA
        //EN DGS002 IDIOMA ES UN JSONARRAY

        private static JSONArray SpecificInformation(XmlNode n)
        {

            XmlNodeList nl = n.SelectNodes("DocumentLanguage");

            JSONArray ja = new JSONArray();
            foreach (XmlNode n0 in nl)
            {
                //ja.Add(SpecificInformation(n0));
                ja.Add(GetObject("SpecificInformation.DocumentLanguage",n0));
            }

            return ja;
        }

        //FIN DE LA MODIFICACION

        private static JSONObject FoodDrinkProduct(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;


            //tag DetailProduct

            nl = n.SelectNodes("DetailProduct");
            jo.Add("Detalle", GetArray("FoodDrinkProduct", nl));
            RemoveNode(n, nl);

        /*    nl = n.SelectNodes("DetailProduct");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodDrinkProduct.DetailProduct", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }
            */


            //LogisticsPackage
            nl = n.SelectNodes("LogisticsPackage");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodDrinkProduct.LogisticsPackage", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //StorageTemperature
            nl = n.SelectNodes("StorageTemperature");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodDrinkProduct.StorageTemperature", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //InvoiceInformation

            nl = n.SelectNodes("InvoiceInformation");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodDrinkProduct.InvoiceInformation", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //otros tags
            ch = GetObject("FoodDrinkProduct", n);
            foreach (string key in ch.Keys)
                jo.Add(key, ch[key]);

            return jo;

        }

        /* fin de la modificacion */

        private static object SanitaryRegister(XmlNodeList nl)
        {
            JSONArray ja = new JSONArray();
            
            JSONObject ch;
            foreach (XmlNode n in nl)
            {
                JSONObject jo = new JSONObject();
                XmlNodeList nl2 = n.SelectNodes("FoodProduct");
                XmlNodeList nl3 = nl2[0].SelectNodes("TradeProduct");
                if (nl3.Count > 0)
                {
                    ch = GetObject("SanitaryRegister.FoodProduct.TradeProduct", nl3[0]);
                    foreach (string key in ch.Keys)
                        jo.Add(key, ch[key]);
                    RemoveNode(nl2[0], nl3);                    
                }
                if (nl2.Count > 0)
                {
                    ch = GetObject("SanitaryRegister.FoodProduct", nl2[0]);
                    foreach (string key in ch.Keys)
                        jo.Add(key, ch[key]);
                    RemoveNode(n, nl2);
                }
                ch = GetObject("SanitaryRegister", n);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                //JSONObject ch = GetObject("AttachedDocument", n);
                ja.Add(jo);
            }
            return ja;
        }

        private static JSONArray FoodIndustrialProduct_s(XmlNodeList nl)
        {
            JSONArray ja = new JSONArray();
            foreach (XmlNode n in nl)
            {
                ja.Add(FoodIndustrialProduct(n));
            }
            return ja;
        }

        /*
        private static JSONArray ProductDestination_s(XmlNodeList nl)
        {
            JSONArray ja = new JSONArray();
            foreach (XmlNode n in nl)
            {
                ja.Add(ProductDestination(n));
            }
            return ja;
        }

        private static JSONObject ProductDestination(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;

            //tag TradeTransportMode
            //nl = n.SelectNodes("TradeTransportMode");
            //jo.Add("TradeTransportMode", GetArray("ProductDestination", nl));
            //RemoveNode(n, nl);

            nl = n.SelectNodes("TradeTransportMode");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.TradeTransportMode", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }


            //tag SenderTradeParty
            nl = n.SelectNodes("SenderTradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.SenderTradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //tag ConsigneeTradeParty
            nl = n.SelectNodes("ConsigneeTradeParty");
            if (nl.Count > 0)
            {
                ch = GetObject("ProductDestination.ConsigneeTradeParty", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            //otros tags
            ch = GetObject("ProductDestination", n);
            foreach (string key in ch.Keys)
                jo.Add(key, ch[key]);

            return jo;
        }
        */

        private static JSONObject FoodIndustrialProduct(XmlNode n)
        {
            JSONObject jo = new JSONObject();
            JSONObject ch;
            XmlNodeList nl;


            // Composition
            nl = n.SelectNodes("Composition");
            jo.Add("Composition", GetArray("FoodIndustrialProduct", nl));
            RemoveNode(n, nl);

            // AdditiveProduct
            nl = n.SelectNodes("AdditiveProduct");
            jo.Add("AdditiveProduct", GetArray("FoodIndustrialProduct", nl));
            RemoveNode(n, nl);

            // LogisticsPackage
            nl = n.SelectNodes("LogisticsPackage");
            jo.Add("LogisticsPackage", GetArray("FoodIndustrialProduct", nl));
            RemoveNode(n, nl);

            // TradeProduct
            nl = n.SelectNodes("TradeProduct");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodIndustrialProduct.TradeProduct", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            // SanitaryRegistryOriginProductRegisterData
            nl = n.SelectNodes("SanitaryRegistryOriginProductRegisterData");
            if (nl.Count > 0)
            {
                jo.Add(GetName("FoodIndustrialProduct.SanitaryRegistryOriginProductRegisterData.RegisterIdentifier"), nl[0].InnerText);
            }

            // ProductClassification
            nl = n.SelectNodes("ProductClassification");
            if (nl.Count > 0)
            {
                ch = GetObject("FoodIndustrialProduct.ProductClassification", nl[0]);
                foreach (string key in ch.Keys)
                    jo.Add(key, ch[key]);
                RemoveNode(n, nl);
            }

            // Los items restantes
            ch = GetObject("FoodIndustrialProduct", n);
            foreach (string key in ch.Keys)
                jo.Add(key, ch[key]);

            return jo;
        }

        private static object AttachedDocument(XmlNodeList nl)
        {
            JSONArray ja = new JSONArray();
            foreach (XmlNode n in nl)
            {
                JSONObject ch = GetObject("AttachedDocument", n);
                ja.Add(ch);
            }
            return ja;
        }

        private static string GetName(string path)
        {
            string n;
            //path = path.ToLower();
            names.TryGetValue(path, out n); //busca en el loadname creado
            return n != null ? n : path.Replace(".", "");
        }

        private static void RemoveNode(XmlNode n, XmlNodeList nl)
        {
            foreach (XmlNode it in nl)
                n.RemoveChild(it);
        }

        private static JSONArray GetArray(string path, XmlNodeList nl)
        {
            //recibe el path con la lista de nodos
            JSONArray ja = new JSONArray();
            foreach (XmlNode it in nl) // para cada nodo dentro de la lista de nodos
            {
                ja.Add(GetObject(path + "." + it.Name, it));
            }
            return ja;
        }

        private static JSONObject GetObject(string path, XmlNode n)
        {

            //recibe el path con un nodo
            JSONObject jo = new JSONObject();
            foreach (XmlNode it in n.ChildNodes) //para cada nodo dentro del principal
            {
                jo.Add(GetName(path + "." + it.Name), it.InnerText);                                   
            }

            return jo;
        }

    }
}
